package com.zhaoliang.demo.quartz;

import java.text.ParseException;
import java.util.Calendar;
import java.util.Date;

import org.quartz.CronTrigger;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.SchedulerFactory;
import org.quartz.SimpleTrigger;
import org.quartz.impl.StdSchedulerFactory;
import org.quartz.impl.calendar.HolidayCalendar;

public class HelloQuartzSchedule {
	
	public static void main(String[] args) throws SchedulerException, ParseException {
		
		SchedulerFactory schedulerFactory = new StdSchedulerFactory();
		
		Scheduler scheduler = schedulerFactory.getScheduler();
		
		JobDetail jobDetail = new JobDetail("helloQuartzJob", Scheduler.DEFAULT_GROUP,HelloQuartzJob.class);
		
		//===============================================
		SimpleTrigger simpleTrigger = new SimpleTrigger("simpleTrigger",Scheduler.DEFAULT_GROUP);
		
		simpleTrigger.setStartTime(new Date(System.currentTimeMillis()));
		simpleTrigger.setRepeatInterval(5000);
		simpleTrigger.setRepeatCount(10);
		
		
		//===============================================
		String cronExpression = "30/5 * * * * ?";
		CronTrigger cronTrigger = new CronTrigger("cronTrigger",Scheduler.DEFAULT_GROUP,cronExpression);
		
		//===============================================
		Calendar calendar = Calendar.getInstance();
		calendar.set(2015, Calendar.DECEMBER, 22);
		HolidayCalendar holidayCalendar = new HolidayCalendar();
		holidayCalendar.addExcludedDate(calendar.getTime());
		scheduler.addCalendar("calendar", holidayCalendar, true, false);
		
		cronTrigger.setCalendarName("calendar");
		
		scheduler.scheduleJob(jobDetail,cronTrigger);// 每分钟的30s起，每5s触发任务  
		//scheduler.scheduleJob(jobDetail,simpleTrigger);
		
		scheduler.start();
		
	}

}
